package com.link2room.rodimus.handler.impl;

import com.link2room.common.constant.DefaultValue;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;
import com.link2room.rodimus.dto.param.base.SignInBaseDto;
import com.link2room.rodimus.dto.param.base.SignInReissueHandlerParamDto;
import com.link2room.rodimus.dto.param.base.SignInReissueRuleShareDto;
import com.link2room.rodimus.entity.SignInReissueBaseEntity;
import com.link2room.rodimus.entity.SignInReissueDetailShareEntity;
import com.link2room.rodimus.handler.ISignInDrawReissueHandler;
import com.link2room.rodimus.service.ISignInService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.redis.util.RedisLockRegistry;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

/**
 * Created by LN on 2018/1/10
 */
@Component("signInDrawReissueShareHandler")
public class SignInDrawReissueShareHandler extends AbstractSignInDrawReissueHandler implements ISignInDrawReissueHandler {
    @Resource
    private ICommonService commonService;
    @Resource
    private ISignInService signInService;
    @Autowired
    private RedisLockRegistry signReissueLcok;
    @Override
    public void signInReissue(String type, SignInReissueHandlerParamDto paramDto) throws Exception {
        String from = paramDto.getFrom();
        String lhotelGroupCode = paramDto.getLhotelGroupCode();
        String headUrl = paramDto.getHeadUrl();
        String memberNo = paramDto.getMemberNo();
        Date month = paramDto.getMonth();
        String nickName = paramDto.getNickName();
        SignInReissueDetailShareEntity signInReissueDetailShareEntity = new SignInReissueDetailShareEntity();
        signInReissueDetailShareEntity.setLhotelGroupCode(lhotelGroupCode);
        signInReissueDetailShareEntity.setMonth(month);
        signInReissueDetailShareEntity.setFromMember(from);
        signInReissueDetailShareEntity.setMemberNo(memberNo);
        signInReissueDetailShareEntity.setHeadUrl(headUrl);
        signInReissueDetailShareEntity.setIsUsed(DefaultValue.F);
        signInReissueDetailShareEntity.setNickName(nickName);
        signInReissueDetailShareEntity.setLhotelGroupCode(lhotelGroupCode);
        try{
            commonService.save(signInReissueDetailShareEntity);
        }catch (Exception e){
            signInReissueDetailShareEntity.setNickName("");
            commonService.save(signInReissueDetailShareEntity);
        }
        SignInBaseDto signInBase = signInService.getUnderwaySignInBaseAll(lhotelGroupCode);
        SignInReissueRuleShareDto ruleShare = signInBase.getSignInReissueRuleShare();
        Integer value = ruleShare.getValue();
        Lock lock = signReissueLcok.obtain(from);

        if(lock.tryLock(30, TimeUnit.SECONDS)){
            try{
                SignInReissueDetailShareEntity find = new SignInReissueDetailShareEntity();
                find.setMonth(month);
                find.setFromMember(from);
                find.setLhotelGroupCode(lhotelGroupCode);
                find.setIsUsed(DefaultValue.F);
                QueryResult<SignInReissueDetailShareEntity> qres = commonService.findAllEQ(SignInReissueDetailShareEntity.class, find, 0, value);
                if(CollectionUtils.isNotEmpty(qres.getResultlist()) && qres.getResultlist().size() == value){
                    qres.getResultlist().forEach(entity->entity.setIsUsed(DefaultValue.T));
                    SignInReissueBaseEntity findReissueBase = new SignInReissueBaseEntity();
                    findReissueBase.setMemberNo(from);
                    findReissueBase.setMonth(month);
                    findReissueBase.setLhotelGroupCode(lhotelGroupCode);
                    SignInReissueBaseEntity reissueBase = commonService.findOneEQ(SignInReissueBaseEntity.class, findReissueBase);
                    if(reissueBase == null){
                        findReissueBase.setTotal(1);
                        findReissueBase.setUsed(0);
                        reissueBase = findReissueBase;
                    }else {
                        reissueBase.setTotal(reissueBase.getTotal() + 1);
                    }
                    signInService.saveReissue(reissueBase,qres.getResultlist());
                }
            }finally {
                lock.unlock();
            }

        }

    }


}
